<?php

namespace App\Http\Controllers;

use App\Models\Article;
use Illuminate\Support\Facades\Auth;
use App\Repositories\ArticlesRepository;
use App\Http\Requests\Logic\Articles\ShowRequest;
use App\Http\Requests\Logic\Articles\QueryResource;
use App\Http\Requests\Logic\Articles\ArticlesCreate;
use App\Http\Requests\Logic\Articles\ArticlesUpdate;
use App\Http\Requests\Logic\Articles\ArticlesDestroy;

class ArticlesController extends Controller
{
    protected $articlesRepository;

    /**
     * ArticlesController constructor.
     * @param ArticlesRepository $articlesRepository
     */
    public function __construct(
        ArticlesRepository $articlesRepository
    )
    {
        $this->articlesRepository = $articlesRepository;
    }

    /**
     * 获取文章资源列表
     *
     * Date: 2018/4/10
     * @author Tenny
     * @param QueryResource $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(QueryResource $request)
    {
        $user = Auth::user();
        $company_id = $user->company_id;
        $limit = $request->get('paginate', 15);
        $title = $request->get('title', false);
        $time = $request->get('time', false);
        $author = $request->get('author', false);
        $articles = $this->articlesRepository->getArticles($title, $time, $author, $company_id, $limit);
        return success($articles);
    }

    /**
     * 根据文章Id 获取文章详情资源
     *
     * Date: 2018/4/10
     * @author Tenny
     * @param ShowRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(ShowRequest $request, $article)
    {
        $res = $this->articlesRepository->getArticlesDetails($article);
        return success($res);
    }


    /**
     * 新建articles资源
     *
     * Date: 2018/4/10
     * @author Tenny
     * @param ArticlesCreate $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function store(ArticlesCreate $request)
    {
        $attributes = $this->extractInputFromRules($request, $request->rules());
        $articles = $this->articlesRepository->create($attributes);
        return stored($articles);
    }

    /**
     * 更新articles资源
     *
     * Date: 2018/4/10
     * @author Tenny
     * @param ArticlesUpdate $request
     * @param Article $article
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(ArticlesUpdate $request, Article $article)
    {
        $articlesAttr = $request->only([
            'title',
            'cover',
            'content',
        ]);
        $article->update($articlesAttr);
        return updated($article);
    }

    /**
     * 删除 articles 资源
     *
     * Date: 2018/4/10
     * @author Tenny
     * @param ArticlesDestroy $request
     * @param Article $article
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function destroy(ArticlesDestroy $request, Article $article)
    {
        $force = $request->get('force');
        $force ? $article->delete() : $article->forceDelete();
        return deleted();
    }

    /**
     * 上传心灵驿站封面图片
     *
     * Date: 2018/5/2
     * @author Tenny
     * @param ArticlesUpload $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function upload(ArticlesUpload $request)
    {
        $path = $request->file('cover')->store('cover');
        return success($path);
    }

}
